package com.example.chao.myphotosearch.adapters;

import android.app.Activity;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.chao.myphotosearch.R;
import com.example.chao.myphotosearch.bean.DownloadImg;
import com.jude.utils.JUtils;

import java.io.File;
import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by chao on 2016/4/12.
 */
public class LargeDownLoadImgPagerAdapter extends PagerAdapter {
    private ArrayList<DownloadImg> downloadImgs;
    private PhotoView photoView;
    private int screenHeight;
    private int screenWidth;
    ViewGroup.LayoutParams mLayoutParams;
    private View view;
    public LargeDownLoadImgPagerAdapter(ArrayList<DownloadImg> downloadImgs) {
        this.downloadImgs = downloadImgs;
        screenHeight = JUtils.getScreenHeight();
        screenWidth = JUtils.getScreenWidth();
    }

    @Override
    public int getCount() {
        return downloadImgs.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, int position) {
        view =  LayoutInflater.from(container.getContext()).inflate(R.layout.item_large_img, null);
        photoView = (PhotoView) view.findViewById(R.id.photoView);
        photoView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
            @Override
            public void onViewTap(View view, float v, float v1) {
                if(container.getContext() instanceof Activity){
                    Activity activity = (Activity) container.getContext();
                    activity.onBackPressed();
                }
            }
        });
//        mLayoutParams = simpleDraweeView.getLayoutParams();
//        if (sosoImages.get(position).getWidth() != 0 && sosoImages.get(position).getHeight() != 0) {
//           /* float mHeight = ((float) (sosoImages.get(position).getHeight()) / ((float) (sosoImages.get(position).getWidth()))) * screenWidth;
//                mLayoutParams.width = screenWidth;
//                mLayoutParams.height = (int) mHeight;*/
//            if(sosoImages.get(position).getHeight()<=sosoImages.get(position).getWidth()*2){
//                float mHeight = ((float) (sosoImages.get(position).getHeight()) / ((float) (sosoImages.get(position).getWidth()))) * screenWidth;
//                mLayoutParams.width = screenWidth;
//                mLayoutParams.height = (int)mHeight;
//            }else{
//                float mWidth =((float) (sosoImages.get(position).getWidth()) / ((float) (sosoImages.get(position).getHeight()))) * screenHeight;
//                mLayoutParams.height = screenHeight;
//                mLayoutParams.width = (int)mWidth;
//            }
//            simpleDraweeView.setLayoutParams(mLayoutParams);
//        }else{
//            mLayoutParams.width = screenWidth;
//            mLayoutParams.height = screenHeight;
//            simpleDraweeView.setLayoutParams(mLayoutParams);
//        }
//        simpleDraweeView.setImageUri(sosoImages.get(position).getLargeImg());
        //glide配合photoview使用更容易适配图片的大小
//        Glide.with(container.getContext())
//                .load(downloadImgs.get(position).getLargUrl())
//                .crossFade()
//                .placeholder(R.drawable.ic_loading)
//                .into(photoView);
        photoView.setImageURI(Uri.fromFile(new File(downloadImgs.get(position).getName())));
//        Log.e("url",sosoImages.get(position).getLargeImg());
        container.addView(view);
//        MyPhotoView photoView = new MyPhotoView(container.getContext());
//        photoView.setImageUri(sosoImages.get(position).getLargeImg());
//        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View)object);
    }
}
